#pragma once

#ifndef DLL
#ifdef _SDB_CLIENT_API_DLL_GENERATION_DIREC

#ifdef __cplusplus
#define DLL extern "C" __declspec(dllexport)
#else
#define DLL __declspec(dllexport)
#endif

#else

#ifdef __cplusplus
#define DLL extern "C" __declspec(dllimport)
#else
#define DLL __declspec(dllimport)
#endif

#endif
#endif

#ifndef _SDB_CLIENT_API_DLL_GENERATION_DIREC
#pragma comment(lib,"SDBClientApi")
#endif


#ifndef _VERB_DEFINE
#define _VERB_DEFINE

#define DEFINE_DEFAULT 1
#define DEFINE_READ 14
#define DEFINE_WRITE 15

#endif

/********************************************************************
 *
 * Connect
 *
 ********************************************************************
 *
 * FUNCTION:
 *
 * Connect to the host indicated by the specified IP and port
 *
 ********************************************************************
 *
 * ARGUMENTS:
 * 
 * [in] ip   - the IP of host, should be in "X.X.X.X" format,
 *             e.g. "192.168.1.3"
 *
 * [in] port - the port the host is listening on
 * 
 * [in] _clientid - default sourceid
 *
 ********************************************************************
 *
 * RETURN: 
 *
 * Zero indicates an successful execution, non-zero otherwise
 *
 ********************************************************************/
DLL int ConnectWithDataBase(const char* ip, int port , int _clientid);

/********************************************************************
 *
 * ConnectWithDataBaseMulIP
 *
 ********************************************************************
 *
 * FUNCTION:
 *
 * Connect to the host indicated by the List IPs and port, 
 * using the first valid IP
 *
 ********************************************************************
 *
 * ARGUMENTS:
 *
 * [in] iplists  - the IP list of host, should be in "X.X.X.X,X.X.X.X" format,
 *             e.g. "192.168.1.3,192.168.1.4"
 *
 * [in] port - the port the host is listening on
 *
 * [in] _clientid - default sourceid
 *
 * [in] bufferiplen - length for validip
 * 
 * [out] validip - the first valid ip
 *
 ********************************************************************
 *
 * RETURN:
 *
 * Zero indicates an successful execution, non-zero otherwise
 *
 ********************************************************************/
DLL int ConnectWithDataBaseMulIP(const char* iplist, int port, int _clientid, int bufferiplen, char* validip);

/********************************************************************
 *
 * Disconnect
 *
 ********************************************************************
 *
 * FUNCTION:
 *
 * Close the connection previously built by function 'Connect'
 *
 ********************************************************************/
DLL void DisconnectWithDataBase();

/********************************************************************
 *
 * GetErrorMessage
 *
 ********************************************************************
 *
 * FUNCTION:
 *
 * Get error message
 *
 ********************************************************************
 *
 * ARGUMENTS:
 * 
 * [in] buffer - the buffer to store error message
 *
 * [in] size   - the size of buffer
 *
 ********************************************************************/
DLL int GetDBErrorMessageA(char *buffer, int size);
DLL int GetDBErrorMessageW(wchar_t *buffer, int size);

/********************************************************************
 *
 * Define
 *
 ********************************************************************
 *
 * FUNCTION:
 *
 * Define a list of data names
 *
 ********************************************************************
 *
 * ARGUMENTS:
 * 
 * [in]  list   - every elemen format by "name/tag/initvalue/unit/upperlmt/lowerlmt/description" and join by ";" when use DefineSimple or verb = 14 or 15
 *
 * [out] id     - the ID of the defined list
 *
 * [in]  number - the number of data items the list will contain
 *
 * [in]  verb   - verb of different type : 1 - Define  14 - DefineRead  15 - DefineWrite
 *
 * [in]  exist  - whether existance is required
 *
 ********************************************************************
 *
 * RETURN:
 *
 * Zero indicates an successful execution, non-zero otherwise
 *
 ********************************************************************/
DLL int DefineDBSimple(int type,const char *list, int verb, int exist, int *id);

DLL int DefineDBIntA(const char* list, int verb, int exist, int* id);
DLL int DefineDBIntW(const wchar_t *list,int verb, int exist, int *id);

DLL int DefineDBByNameIntA(const char *list, int number, int verb, int exist, int *id);
DLL int DefineDBByNameIntW(const wchar_t *list, int number, int verb, int exist, int *id);


DLL int DefineDBDoubleA(const char *list, int verb, int exist, int *id);
DLL int DefineDBDoubleW(const wchar_t *list, int verb, int exist, int *id);

DLL int DefineDBByNameDoubleA(const char *list, int number, int verb, int exist, int *id);
DLL int DefineDBByNameDoubleW(const wchar_t *list, int number, int verb, int exist, int *id);

/********************************************************************
 *
 * Select
 *
 ********************************************************************
 *
 * FUNCTION:
 *
 * Read data from server database
 *
 ********************************************************************
 *
 * ARGUMENTS:
 * 
 * [in] id   - the ID of a predefined defined list
 *
 * [in] list - the names of data to read
 *
 * [in] data - the buffer to store data read from server database
 *
 * [in] len  - the length of buffer
 *
 ********************************************************************
 *
 * RETURN:
 *
 * Non-negative number indicates the number of data values selected 
 * from server database, a negative number means error occurred
 *
 ********************************************************************/
DLL	int SelectDBIDInt(int id, int *data, int len);
DLL	int SelectDBListIntA(const char *list, int *data, int len);
DLL int SelectDBListIntW(const wchar_t *list, int *data, int len);

DLL	int SelectDBIDDouble(int id, double *data, int len);
DLL	int SelectDBListDoubleA(const char *list, double *data, int len);
DLL	int SelectDBListDoubleW(const wchar_t *list, double *data, int len);

/********************************************************************
 *
 * Update
 *
 ********************************************************************
 *
 * FUNCTION:
 *
 * Update data of server database
 *
 ********************************************************************
 *
 * ARGUMENTS:
 * 
 * [in] id   - the ID of a predefined defined list
 *
 * [in] list - the names of data to update
 *
 * [in] data - the data values to update
 *
 * [in] len  - the count of data values
 *
 ********************************************************************
 *
 * RETURN:
 *
 * Zero indicates an successful execution, non-zero otherwise
 *
 ********************************************************************/
DLL	int UpdateDBIDInt(int id, const int *data, int len);
DLL	int UpdateDBListIntA(const char *list, const int *data, int len);
DLL	int UpdateDBListIntW(const wchar_t *list, const int *data, int len);

DLL	int UpdateDBIDDouble(int id, const double *data, int len);
DLL	int UpdateDBListDoubleA(const char *list, const double *data, int len);
DLL	int UpdateDBListDoubleW(const wchar_t *list, const double *data, int len);